<template>
  <div>
    <!-- 表格 -->
    <el-table style="width: 100%" border :data="records">
      <el-table-column type="index" :index="index1" label="序号" width="80px" align="center"> </el-table-column>
      <el-table-column prop="skuName" label="名称" width="width"> </el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="width"> </el-table-column>
      <el-table-column label="默认图片" width="100px">
        <template v-slot:default="{ row, $index }">
          <img :src="row.skuDefaultImg" alt="" style="width: 80px; height: 80px" />
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量(KG)" width="80px"> </el-table-column>
      <el-table-column prop="price" label="价格(元)" width="80px"> </el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template #default="{ row, $index }">
          <el-button type="success" icon="el-icon-bottom" size="mini" title="下架商品" v-if="row.isSale == 0" @click="sale(row)"></el-button>
          <el-button type="success" icon="el-icon-top" size="mini" title="上架商品" v-else @click="cancle(row)"></el-button>
          <el-button type="primary" icon="el-icon-edit" size="mini" title="修改商品" @click="edit"></el-button>
          <el-button type="info" icon="el-icon-info" size="mini" title="查看商品" @click="getSkuInfo(row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" title="删除商品" @click="deleteSku(row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination :current-page="page" :page-sizes="[3, 5, 10]" :page-size="limit" layout=" prev, pager, next, jumper, ->,sizes,total" :total="total" style="text-align: center" @size-change="handleSizeChange" @current-change="handleCurrentChange"> </el-pagination>
    <!-- 抽屉效果展示 -->
    <el-drawer title="我是标题" :visible.sync="drawer" :before-close="handleClose" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <template>
            <el-tag type="success" v-for="(attr, index) in skuInfo.skuAttrValueList" :key="attr.id" style="margin-right: 10px">{{ attr.attrName }}---{{ attr.valueName }}</el-tag>
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台图片</el-col>
        <el-col :span="16">
          <el-carousel height="150px">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img :src="item.imgUrl" alt="" />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: 'Sku',
  data() {
    return {
      page: 1,
      limit: 3,
      records: [], //存储sku列表的数据
      total: 0, //总页数
      skuInfo: {}, //存储sku的详情信息
      drawer: false, //控制抽屉的显示与隐藏
    }
  },
  created() {
    //获取sku列表的数据
    this.getSkuList()
  },
  methods: {
    //获取sku列表的数据
    async getSkuList() {
      let res = await this.$api.sku.reqSkuList1({ page: this.page, limit: this.limit })
      if (res.code == 200) {
        this.records = res.data.records
        this.total = res.data.total
      }
    },
    index1(index) {
      return index + 1 + (this.page - 1) * this.limit
    },
    //修改当前的展示数量
    handleSizeChange(limit) {
      this.limit = limit
      this.getSkuList()
    },
    //修当前也=页码
    handleCurrentChange(page) {
      this.page = page
      this.getSkuList()
    },
    //上架商品
    async sale(row) {
      let res = await this.$api.sku.reqSale({ skuId: row.id })
      if (res.code == 200) {
        row.isSale = 1
        this.$message({ type: 'success', message: '商品上架成功' })
        // this.getSkuList()
      }
    },
    //下架商品
    async cancle(row) {
      let res = await this.$api.sku.reqCancle({ skuId: row.id })
      if (res.code == 200) {
        row.isSale = 0
        this.$message({ type: 'success', message: '商品下架成功' })
        // this.getSkuList()
      }
    },
    //修改商品详情
    edit() {
      this.$message('正在开发中')
    },
    //查看商品详情
    async getSkuInfo(row) {
      let res = await this.$api.sku.reqSkuById({ skuId: row.id })
      if (res.code === 200) {
        this.skuInfo = res.data
      }
      this.drawer = true
    },
    // 关闭抽屉的函数
    handleClose() {
      this.drawer = false
    },
    async deleteSku(row) {
      let res = await this.$api.sku.reqDeleteSku({ skuId: row.id })
      if (res.code == 200) {
        this.$message({ type: 'success', message: '删除成功' })
        this.page = this.records.length > 1 ? this.page : this.page - 1
        this.getSkuList()
      }
    },
  },
}
</script>

<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>

<style>
.el-row .el-col-5 {
  font-size: 18px;
  text-align: right;
}
.el-col {
  margin: 10px 10px;
}
.el-carousel__button {
  width: 10px;
  height: 10px;
  background-color: red;
  border-radius: 50%;
}
</style>